package reentlock;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

public class NoteSafe {
    private static Integer n = 5;
    public static void main(String[] args) {
        ExecutorService exe = new ThreadPoolExecutor(5, 10, 5, TimeUnit.SECONDS, new LinkedBlockingDeque<>());
        ReentrantLock reentrantLock = new ReentrantLock();
        for (int i = 0; i < 50; i++) {
            exe.execute(() -> {
                try {
                    reentrantLock.lock();
                    if (n > 0) {
                        Thread.sleep(500);
                        n--;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    reentrantLock.unlock();
                }
            });
        }
        exe.shutdown();
        while (!exe.isTerminated()){
        }
        System.out.println(n);
    }
}
